#include <stdio.h>
#include <iostream>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#define PORT 8001
using namespace std;
int main(int argc, char *argv[])
{
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if (lfd < 0)
    {
        cout << "chen" << endl;
        return 1;
    }
    cout << "socket success" << endl;
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(PORT);
    local.sin_addr.s_addr = 0;
    int ans = bind(lfd, (struct sockaddr *)&local, sizeof(local));
    if (ans < 0)
    {
        cout << "biao" << endl;
        return 1;
    }
    cout << "bind success" << endl;
    ans = listen(lfd, 128);
    if (ans < 0)
    {
        cout << "ren" << endl;
        return 1;
    }
    cout << "listen success" << endl;
    int maxfd = lfd; // 最大的文件描述符
    fd_set oldset, rset;
    FD_ZERO(&oldset);
    FD_ZERO(&rset);

    // 将lfd添加oldset集合中
    FD_SET(lfd, &oldset);

    while (1)
    {
        rset = oldset; // 将oldset赋值给需要监听的rset集合
        int n = select(maxfd + 1, &rset, NULL, NULL, NULL);
        if (n < 0)
        {
            perror("错误!");
            break;
        }
        else if (n == 0)
        {
            continue; // 没有变化 继续监听
        }
        else // 监听到了文件描述符的变化
        {
            // lfd变化 代表有新的连接到来
            if (FD_ISSET(lfd, &rset))
            {
                struct sockaddr_in cliaddr;
                socklen_t len = sizeof(cliaddr);
                char ip[16] = "";
                // 提取新的连接
                int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);
                printf("new client ip = %s  port = %d \n", inet_ntop(AF_INET, &(cliaddr.sin_addr.s_addr), ip, 16), ntohs(cliaddr.sin_port));

                // 将cfd添加至oldset集合中,以下次监听
                FD_SET(cfd, &oldset);

                if (cfd > maxfd)
                    maxfd = cfd;
                // 如果只有lfd变化 continue
                if (--n == 0)
                    continue;
            }
            // cfd 遍历lfd之后的文件描述符是否在rset集合中,如果在则cfd变化
            for (int i = lfd + 1; i <= maxfd; i++)
            {
                if (FD_ISSET(i, &rset))
                {
                    char buffer[1500];
                    int ret = read(i, buffer, sizeof(buffer));
                    if (ret < 0)
                    {
                        perror("read 错误!");
                        close(i);
                        FD_CLR(i, &oldset);
                    }
                    else if (ret == 0)
                    {
                        printf("client close\n");
                        close(i);
                        FD_CLR(i, &oldset);
                    }
                    else
                    {
                        buffer[ret] = 0;
                        printf("读到的内容:%s", buffer);
                        write(i, buffer, ret);
                    }
                }
            }
        }
    }
    return 0;
}